
//
//  BgoodsAdminViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/16.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BgoodsAdminViewController.h"
#import "BgoodsLeftViewController.h"
#import "BgoodsrightViewController.h"
#import "BEditShopDetaileViewController.h"

static int const labelWith = 60;

@interface BgoodsAdminViewController ()<UIScrollViewDelegate>{
    UILabel *mylab;
}
@property(nonatomic,weak)UIScrollView *titleScrollView;
@property(nonatomic,weak)UIScrollView *contentScrollView;
@property(nonatomic,strong)UIButton *oldButton;
@property(nonatomic,strong)UILabel *seletLabel;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,assign)BOOL isbutton;
@property(nonatomic,strong)UILabel *leftLabel;
@property(nonatomic,strong)UILabel *righttLabel;

@end

@implementation BgoodsAdminViewController

- (void)viewWillAppear:(BOOL)animated{
    [MNFloatBtn show];
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MNFloatBtn hidden];
}

-(NSMutableArray *)titleArray{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isbutton = NO;
    self.title = @"产品管理";
    // Do any additional setup after loading the view.
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setScrollViewTopandBottom];
    [self setupViewControllers];
    [self setTitlesButton];
    [self.rightbutton setTitle:@"发布" forState:(UIControlStateNormal)];
    [self.rightbutton setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sholable:) name:@"tongzhiRefish" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shorightlable:) name:@"tongzhiright" object:nil];
    
    [MNFloatBtn show];
    MNFloatBtn *btn = [MNFloatBtn getFloatBtn];
    [btn setTitle:@"" forState:UIControlStateNormal];
    btn.btnClick = ^(UIButton *sender) {
        [self.navigationController pushViewController:[BEditShopDetaileViewController new] animated:YES];
    };
    [MNFloatBtn hidden];
}

- (void)rightClick{
    [self.navigationController pushViewController:[BEditShopDetaileViewController new] animated:YES];
}

-(void)sholable:(NSNotification *)noti{
    NSString *strleft = [NSString stringWithFormat:@"(%@)", [noti.userInfo objectForKey:@"two"]];
    self.leftLabel.text =strleft;
}

-(void)shorightlable:(NSNotification *)noti{
    NSString *strleft = [NSString stringWithFormat:@"(%@)", [noti.userInfo objectForKey:@"one"]];
    self.righttLabel.text =strleft;
}

-(void)returnaction{
    [self.navigationController popViewControllerAnimated:YES];
}

//初始化标题ScrollView
-(void)setupScrollView{
    NSInteger count = self.childViewControllers.count;
    //设置标题滚动条
    self.titleScrollView.contentSize = CGSizeMake(count * labelWith, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    //设置内容滚动条
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*count, 0);
    //开启分页
    self.contentScrollView.pagingEnabled = YES;
    //没有弹簧效果
    self.contentScrollView.bounces = NO;
    //隐藏水平滚动条
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //设置协议
    self.contentScrollView.delegate = self;
}

//设置页面scrollView
-(void)setScrollViewTopandBottom{
    
    UIScrollView *contentScroll = [[UIScrollView alloc]init];
    contentScroll.frame = CGRectMake(0, kNaviHeight+47, ScreenW, KHEIGHT-kNaviHeight
                                     -47);
    contentScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentScroll];
    self.contentScrollView = contentScroll;
    self.contentScrollView.userInteractionEnabled = YES;
    self.contentScrollView.scrollEnabled = YES;;
    self.contentScrollView.pagingEnabled = YES;
    //显示水平方向的滚动条(默认YES)
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //显示垂直方向的滚动条(默认YES)
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*2, ScreenH-kNaviHeight-44);
    self.contentScrollView.delegate = self;
}

#pragma mark -- UISCrollview代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    //  CGFloat offsetX = index * ScreenW;
    UIButton *but = (UIButton *)[self.view viewWithTag:100+index];
    [self.oldButton setTitleColor:[self colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    [but setTitleColor:zhutiColor forState:UIControlStateNormal];
    self.oldButton = but;
    CGPoint CEN =  mylab.center;
    CEN.x = but.center.x;
    mylab.center = CEN;
    
    if (index == 0) {
        self.leftLabel.textColor = zhutiColor;
        self.righttLabel.textColor = K666666;
    } else {
        self.righttLabel.textColor = zhutiColor;
        self.leftLabel.textColor = K666666;
    }
}

-(void)setTitlesButton{
    NSArray *arr = @[@"出售中",@"已下架"];
    CGFloat leftRight = (KWIDTH-110)/4;
    CGFloat wwww = 60;
    CGFloat hhhhh = 30;
    CGFloat yyyy = kNaviHeight +12;
    
    for (int i = 0; i<arr.count; i++) {
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake((leftRight*2+wwww)*i+leftRight, yyyy, wwww, hhhhh);
        [self.view addSubview:myButton];
        myButton.tag = 100+i;
        [myButton setTitle:arr[i] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [myButton setTitleColor:K666666 forState:UIControlStateNormal];
        myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        myButton.titleLabel.font = FontSize(15);

        if (i == 0) {
            [myButton setTitleColor:zhutiColor forState:UIControlStateNormal];

            self.oldButton = myButton;
            [self titleClick:myButton];
            mylab = [[UILabel alloc]init];
            [self.view addSubview:mylab];
            mylab.frame = CGRectMake(10, yyyy+hhhhh, 60, 2);
            CGPoint CEN =  mylab.center;
            CEN.x = myButton.center.x;
            mylab.center = CEN;
            mylab.backgroundColor = zhutiColor;
        }
        
        UILabel *numberLb = [[UILabel alloc]init];
        [self.view addSubview:numberLb];
        [numberLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kNaviHeight+14);
            make.left.offset((leftRight*2+wwww)*i+leftRight+wwww-2);
            make.height.offset(10);
            
        }];
        numberLb.font = FontSize(10);
        numberLb.textColor = K666666;
        numberLb.text = @"(0)";
        if (i == 0) {
            self.leftLabel = numberLb;
            numberLb.textColor = zhutiColor;
        } else {
            self.righttLabel = numberLb;
        }
    }
}

-(void)titleClick:(UIButton *)but{
    //0取出label
    NSInteger index = but.tag-100;
    //2.1计算滚动位置
    CGFloat offsetX = index * ScreenW;
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

//显示页面
-(void)showVC:(NSInteger)index{
    
    CGFloat offsetX = index * ScreenW;
    UIViewController *vc = self.childViewControllers[index];
    //判断当前控制器的View 有没有加载过 如果已经加载过 就不需要加载
    if (vc.isViewLoaded) return;
    vc.view.frame = CGRectMake(offsetX, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:vc.view];
}

- (void)setupViewControllers {
    BgoodsLeftViewController *seveVC = [[BgoodsLeftViewController alloc]init];
    seveVC.view.frame = CGRectMake(0, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:seveVC.view];
    [self addChildViewController:seveVC];
    
    BgoodsrightViewController *evaluate = [[BgoodsrightViewController alloc]init];
    evaluate.view.frame = CGRectMake(ScreenW, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:evaluate.view];
    [self addChildViewController:evaluate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
